package com.mall.biz.trade.impl.o2o.incluce;


import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mall.biz.trade.O2OTradeBiz;
import com.mall.dto.TradePriceAndBuyCountDTO;
import com.mall.pojo.Trade;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.MoneyUtil;


@Service
public class MaiDanTradeImpl implements O2OTradeBiz {

	@Override
	public TradePriceAndBuyCountDTO pack4PirceAndBuyCount(HttpServletRequest request, Integer type, Long price,
			Integer buyCount, Long parentId) {
		TradePriceAndBuyCountDTO tradePriceAndBuyCountDTO =  new TradePriceAndBuyCountDTO();
		tradePriceAndBuyCountDTO.setBuyCount(1);
		tradePriceAndBuyCountDTO.setPrice(MoneyUtil.getDBMoney(price));
		//折扣价格
		Long discountPrice = 0l;
		Long notDisPrice = HttpUtil.getLongValueFromRequest(request, "notDisPriceStr");// 不参与优惠的金额
		Long orMoney = HttpUtil.getLongValueFromRequest(request, "orMoneyStr");// 原价
		if (notDisPrice == null) {
			notDisPrice=0l;
		}
		if (orMoney!=null && orMoney>0) {
			if (orMoney > notDisPrice) {
				discountPrice = orMoney - notDisPrice - tradePriceAndBuyCountDTO.getPrice();
			} else {
				discountPrice = 0l;// 如果消费总金额小于或等于不参加优惠的金额，则优惠金额为0
			}
		}
		tradePriceAndBuyCountDTO.setDiscountPrice(discountPrice);
		return tradePriceAndBuyCountDTO;
	}

	@Override
	public Boolean beforeInsert(HttpServletRequest request,Trade trade) {
		Long notDisPrice = HttpUtil.getLongValueFromRequest(request, "notDisPriceStr");// 不参与优惠的金额
		Long orMoney = HttpUtil.getLongValueFromRequest(request, "orMoneyStr");// 原价
		HashMap hm = new HashMap();
		hm.put("maiDanOriginalMoney", new Long(orMoney));
		hm.put("maiDanNotDisPrice", new Long(notDisPrice));
		String attribute = JSON.toJSONString(hm);
		trade.setAttribute(attribute);
		return true;
	}

	@Override
	public Boolean afterSuccInsert(HttpServletRequest request, Trade trade) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean afterErrorInsert(HttpServletRequest request, Trade trade) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
